package com.cyyl.updater

import android.content.Context
import com.cyyl.updater.browser.BrowserUpdate
import kotlinx.coroutines.flow.MutableStateFlow

/**
 * Created on 2019/12/23
 * Title:
 * Description:
 *
 * @author Android-张康
 */
class AppUpdaterBuilder(
    context: Context,
    updateType: Int = AppUpdater.UPDATE_TYPE_INTERNAL
) {

    private var mContext: Context = context
    private var mUpdateType: Int = updateType

    private var mPackageName: String? = null

    private var mAppName: String? = null

    private var mUpdateWebLinks: String? = null

    private var mBrowserType: Int = BrowserUpdate.BROWSER_TYPE_DEFAULT

    private var mDownloadUrl: String? = null

    private var mFileName: String? = null

    private var mTitle: String? = null

    private var mDescription: String? = null

    internal val downloadProgressFlow: MutableStateFlow<Triple<Int, Int, String?>> by lazy {
        MutableStateFlow(Triple(-1, -1, null))
    }

    fun getContext() = mContext
    fun getUpdateType() = mUpdateType
    fun getPackageName() = mPackageName

    fun setPackageName(packageName: String): AppUpdaterBuilder {
        mUpdateType = AppUpdater.UPDATE_TYPE_MARKET
        mPackageName = packageName
        return this
    }

    fun getAppName() = mAppName

    fun setAppName(appName: String): AppUpdaterBuilder {
        mUpdateType = AppUpdater.UPDATE_TYPE_MARKET
        mAppName = appName
        return this
    }

    fun getUpdateWebLinks() = mUpdateWebLinks

    fun getBrowserType() = mBrowserType

    fun setUpdateWebLinks(
        updateWebLinks: String,
        browserType: Int = AppUpdater.BROWSER_TYPE_DEFAULT
    ): AppUpdaterBuilder {
        mUpdateType = AppUpdater.UPDATE_TYPE_BROWSER
        mUpdateWebLinks = updateWebLinks
        mBrowserType = browserType
        return this
    }

    fun getDownloadUrl() = mDownloadUrl

    fun setDownloadUrl(downloadUrl: String): AppUpdaterBuilder {
        mUpdateType = AppUpdater.UPDATE_TYPE_INTERNAL
        mDownloadUrl = downloadUrl
        return this
    }

    fun getFileName() = mFileName

    fun setFileName(fileName: String): AppUpdaterBuilder {
        mFileName = fileName
        return this
    }

    fun getTitle() = mTitle

    fun setTitle(title: String): AppUpdaterBuilder {
        mTitle = title
        return this
    }

    fun getDescription() = mDescription

    fun setDescription(description: String): AppUpdaterBuilder {
        mDescription = description
        return this
    }

    fun build(): AppUpdater {
        return AppUpdater(this)
    }

    /**
     * 更新
     *
     */
    fun update(): AppUpdater {
        return AppUpdater(this).also {
            it.update()
        }
    }

}